//导入
// @koala-prepend "../../libs/swiper.jquery.min.js"

var homehref = '';
$(function(){

    Page.id = urls.GetQueryString('key');
    
    var url_param = '?key=' + Page.id;
    $('.tab-canshu').attr('href', '../canshu/' + url_param);
    $('.tab-pingfen').attr('href', '../pingfen/'  + url_param);      

    Page.loadData();
    Page.loadImg();
    Page.initcss();

    $('.param .tab .title li').click(function(){
        var me = $(this);
        me.addClass('action').siblings('li').removeClass('action');
        $(me.attr('data-target')).show().siblings('.tab-content').hide();
    });

    $('.container-bigimg .return').click(function(){
        $('.container-bigimg').hide();
    });            
});

var Page = {
    id: false,
    name: false,
    parent_id: false,
    parent_name: false
};
var mySwiper;
Page.initcss = function() {
    var bodyheight = Page.getWindowHeight();
    $('.bigimg').height(bodyheight - 40);
    $('.container-bgimg').css("min-height", bodyheight+"px");
}


Page.loadData = function() {
    var url = urls.server + '/bikes/' + Page.id + '';
    $.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        data: {
            id: Page.id
        },
        success: function(e){
            if(!e || !e.id ) {
                Page.alert('数据加载失败');
                return;
            }
            $('.param').show();
            $('.moto-title').html(e.year + ' ' + e.brand + ' ' + e.name);
            $('.moto-name').html(e.name);
            $('.moto-number').html(e.number);
            $('.moto-year').html(e.year);
            $('.moto-real-km').html(e.real_km);
            $('.moto-brand').html(e.brand);
            $('.moto-cat').html(e.category);
            $('.moto-price').html( Number(e.price)/10000 + '万元' );
            $('.moto-viewchange').html( e.view_change ? e.view_change : '' );
            $('.moto-diy').html( e.diy ? e.diy : '' );
            $('.moto-merchants-number').html( e.merchant_num ? e.merchant_num : '' );
            // $('.preview-list-img').attr('src', e.image_url);
            $('.moto-price-range').html( (e.price_range_min/10000) + '~' + (e.price_range_max/10000) + '万元' );
            if(e.sale_state == '0') {
                $('.sale-state').show();
            }
            Page.showSData(e);

            if(e.param_link_model && e.param_link_model.length > 0) {
                $('.select-modal').html( e.param_link_model[0].name + '>>').attr('data-id', e.param_link_model[0].id);  
                var url_param = '?key=' + Page.id + '&mkey=' + e.param_link_model[0].id;
                $('.tab-canshu, .to-canshu a').attr('href', '../canshu/' + url_param);
                $('.tab-pingfen').attr('href', '../pingfen/'  + url_param);                  
            }
        }
    });
}

Page.showSData = function(e) {
    var h = '';
    var data_level = e.samelevel;
    for( var i=0, len=data_level.length; i<len; i++ ) {
        var item = data_level[i];

        h += '<li>';
        h += '  <div>';
        var img = item.image_url ? item.image_url : '..\/imgs\/404.jpg';
        h += '      <img src="'+ img +'">';
        h += '      <div class="mname">'+ item.year + ' ' + item.brand + ' ' + item.name +'</div>';
        h += '      <div class="mprice">'+ Number(e.price)/10000 +'万元</div>';
        h += '      <div class="minfo">'+ item.year+'/' + item.real_km +'</div>';
        h += '  </div>';
        h += '</li>'
    }

    $('.tab-content.tab-level ul').html(h);

    h = '';
    var data_price = e.sameprice;
    for( var i=0, len=data_price.length; i<len; i++ ) {
        var item = data_price[i];

        h += '<li>';
        h += '  <div>';
        var img = item.image_url ? item.image_url : '..\/imgs\/404.jpg';
        h += '      <img src="'+ img +'">';
        h += '      <div class="mname">'+ item.year + ' ' + item.brand + ' ' + item.name +'</div>';
        h += '      <div class="mprice">'+ Number(e.price)/10000 +'万元</div>';
        h += '      <div class="minfo">'+ item.year+'/' + item.real_km +'</div>';
        h += '  </div>';
        h += '</li>'
    }

    $('.tab-content.tab-price ul').html(h);
}

Page.main_pics = [];
Page.old_pics = [];
Page.loadImg = function() {
    // var url = urls.server;
    // url += '/MotoModelSeries?';
    // url += '&filter[fields][listImage]=true';
    // url += '&filter[where][id]=' + Page.id;

    
    var url = urls.server;
    // url += '/MotoModelSeries?filter[fields][id]=true&filter[fields][name]=true';
    // url += '&filter[fields][listImage]=true&filter[fields][brand]=true&filter[fields][cat]=true';
    // url += '&filter[fields][rate]=true';
    // url += '&filter[limit]=50&filter[where][id]=' + Page.id;

    url += '/bikes/' + Page.id + '/images';

    $.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        success: function(e){
            if( !e || e.length == 0 ) {
                $('.preview-list-img').attr('src', '../imgs/404.jpg');
                return;
            }
            var main_pics = [], old_pics = [], h = '';
            for( var i=0, len=e.length; i<len; i++ ) {
                var item = e[i];
                if(item.category == '1') {
                    Page.main_pics.push(item);
                }
                else {
                    Page.old_pics.push(item);
                }
            }

            if( Page.main_pics.length > 0) {
                Page.changeImges(1);
            }

            if(Page.old_pics.length > 0) {
                $('.change-imgs').attr('data-type', 0).show().unbind('click').click(function(){
                    var type = $(this).attr('data-type');
                    Page.changeImges(type);
                    $(this).attr('data-type', (type==1 ? 0 : 1) ).html( type==0 ? '显示实车图>' : '显示原车图>' )
                });
            }
        }
    });
}

Page.alert = function(str) {
    $('.modal-alert .btn-modal-alert').unbind('click').click(function(){
        $('.modal-alert').hide();
    })
    $('.modal-alert .modal-title').html(str);
    $('.modal-alert').show();
}


Page.goToSlide = function() {
    $('.container-bigimg').show();
    mySwiper.slideTo(0, 100, false);
    $('.current').html('1');
}

Page.nextImage = function(swiper) {
    $('.current').html(swiper.activeIndex+1);
}

Page.changeImges = function(type) {
    $('.preview-list-img').removeAttr('data-exist');
    var data = type==1 ? Page.main_pics : Page.old_pics, h='';
    for( var i=0, len=data.length; i<len; i++ ) {
        var item = data[i];

        if( item.main_pic == '1' ) {
            $('.preview-list-img').attr({'src':item.image_url, 'data-exist': 'true'});
        }

        h += '<div class="swiper-slide">';
        h += '  <img class="big-image" src="'+ item.image_url +'">';
        h += '</div>';
    }
    if( !$('.preview-list-img').attr('data-exist') ) {
        $('.preview-list-img').attr( data[0].image_url );
    }
    $('.swiper-wrapper').html(h);
    mySwiper = null;
    mySwiper = new Swiper('.swiper-container', {
        direction: 'horizontal',
        initialSlide: 0,
        loop: false,
        observer:true,
        observeParents:true,
        onSlideChangeEnd: Page.nextImage
    });
    $('.imgdetail').unbind('click').click(Page.goToSlide);
    $('.counts').html('/'+ data.length);
}



//获取页面显示区域高度
Page.getWindowHeight = function() {
    if (navigator.appName == "Microsoft Internet Explorer" ) {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    }
    else {
        return self.innerHeight;
    } 
}

//获取页面显示区域宽度
Page.getWindowWidth = function() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    }
    else {
        return self.innerWidth;
    } 
}

